<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Login Page</title>
    <style>
        body {
            font-family: Arial, sans-serif;
            display: flex;
            justify-content: center;
            align-items: center;
            height: 100vh;
            background-color: #f4f4f4;
            margin: 0;
        }
        .login-container {
            background: #fff;
            padding: 2rem;
            border-radius: 8px;
            box-shadow: 0 0 10px rgba(0, 0, 0, 0.1);
            width: 100%;
            max-width: 300px;
        }
        h1 {
            margin-top: 0;
            font-size: 24px;
            color: #333;
        }
        label {
            display: block;
            margin-bottom: 8px;
            font-weight: bold;
        }
        input[type="text"], input[type="password"] {
            width: 100%;
            padding: 10px;
            margin-bottom: 1rem;
            border: 1px solid #ccc;
            border-radius: 4px;
        }
        button {
            width: 100%;
            padding: 10px;
            border: none;
            border-radius: 4px;
            background-color: #007bff;
            color: #fff;
            font-size: 16px;
            cursor: pointer;
        }
        button:hover {
            background-color: #0056b3;
        }
        .message {
            color: #d9534f;
            margin-top: 1rem;
        }
    </style>
</head>
<body>
<div class="login-container">
    <h1>Login</h1>
    <form id="loginForm" onsubmit="return handleSubmit(event)">
        <label for="account">账户:</label>
        <input type="text" id="account" name="account" required>
        <label for="password">密码:</label>
        <input type="password" id="password" name="password" required>
        <button type="submit">登录</button>
        <div id="message" class="message"></div>
    </form>
</div>

<script>
    async function handleSubmit(event) {
        event.preventDefault();

        const account = document.getElementById('account').value;
        const password = document.getElementById('password').value;

        try {
            const response = await fetch('/User/login', {
                method: 'POST',
                headers: {
                    'Content-Type': 'application/json'
                },
                body: JSON.stringify({ account, password })
            });

            if (response.ok) {
                const result = await response.json();
                if (result) {
                    window.location.href = '/success.html';
                    document.getElementById('message').textContent = '登录成功!';
                    document.getElementById('message').style.color = 'green';
                } else {
                    document.getElementById('message').textContent = '登录失败';
                    document.getElementById('message').style.color = 'red';
                }
            } else {
                document.getElementById('message').textContent = '服务错误，请重试';
                document.getElementById('message').style.color = 'red';
            }
        } catch (error) {
            document.getElementById('message').textContent = 'An error occurred. Please try again.';
            document.getElementById('message').style.color = 'red';
        }
    }
</script>
</body>
</html>
